package cn.drenal.capcap.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.drenal.capcap.common.Response;
import cn.drenal.capcap.entity.Banner;
import cn.drenal.capcap.service.BannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 轮播图管理控制器
 */
@RestController
@RequestMapping("/banner")
@RequiredArgsConstructor
public class BannerController {

    private final BannerService bannerService;

    /**
     * 分页查询轮播图列表
     *
     * @param current 当前页
     * @param size    每页大小
     * @param title   轮播图标题
     * @param status  轮播图状态
     * @return 分页数据
     */
    @GetMapping("/page")
    @SaCheckPermission("homepage:banner:list")
    public Response<Page<Banner>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer status
    ) {
        return Response.success(bannerService.page(current, size, title, status));
    }

    /**
     * 获取轮播图详情
     *
     * @param id 轮播图ID
     * @return 轮播图详情
     */
    @GetMapping("/{id}")
    @SaCheckPermission("homepage:banner:list")
    public Response<Banner> getById(@PathVariable Long id) {
        return Response.success(bannerService.getById(id));
    }

    /**
     * 新增轮播图
     *
     * @param banner 轮播图信息
     * @return 操作结果
     */
    @PostMapping
    @SaCheckPermission("homepage:banner:add")
    public Response<Void> add(@RequestBody Banner banner) {
        bannerService.save(banner);
        return Response.success();
    }

    /**
     * 更新轮播图
     *
     * @param id     轮播图ID
     * @param banner 轮播图信息
     * @return 操作结果
     */
    @PutMapping("/{id}")
    @SaCheckPermission("homepage:banner:edit")
    public Response<Void> update(@PathVariable Long id, @RequestBody Banner banner) {
        banner.setId(id);
        bannerService.updateById(banner);
        return Response.success();
    }

    /**
     * 删除轮播图
     *
     * @param id 轮播图ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("homepage:banner:delete")
    public Response<Void> delete(@PathVariable Long id) {
        bannerService.removeById(id);
        return Response.success();
    }
}